class StakeholdersController < ApplicationController
  def new
    @stakeholder = Stakeholder.new
    @stakeholder.project_id = current_user.current_project_id
    if Stakeholder.last.nil?
      @id = params[:no].to_i
    else
      @id = Stakeholder.last.id+1+params[:no].to_i; 
    end  
    
    render :layout => nil
  end
  
  def create
    @stakeholder = Stakeholder.new(:name => params[:name], :description => params[:description], :project_id => current_user.current_project_id)
    if @stakeholder.save
      @row = "<tr class=\"stakeholder\" id=\"sth_" + @stakeholder.id.to_s + "\">" +
                 "<td>" + @stakeholder.name + "</td>" +
                 "<td>" + @stakeholder.description + "</td>" +
              "</tr>"
      render :layout => nil, :text => @row
    else
      #Errorhandling via JS
      render :layout => nil, :text => "1"
    end
  end
  
  def update
    @stakeholder = Stakeholder.find(params[:id])

     if @stakeholder.update_attributes(:name => params[:name], :description => params[:description], :project_id => current_user.current_project_id)
        @row = "<tr class=\"stakeholder\" id=\"sth_" + @stakeholder.id.to_s + "\">" +
                 "<td>" + @stakeholder.name + "</td>" +
                 "<td>" + @stakeholder.description + "</td>" +
              "</tr>"        
        render :layout => nil, :text => @row
      else
        render :layout => nil, :text => "1"
      end
  end
  
  def show
    new_project_id = params[:active_project_id]
    
    unless new_project_id.nil?
        
    end
    @activeProject = params[:active_project_id]
    unless @activeProject.nil?
      current_user.update_attribute(:current_project_id, @activeProject)
      render :layout => nil
    else
       @activeProject = current_user.current_project_id
    end
  end

  def edit
    @stakeholder = Stakeholder.find(params[:id])
    render :layout => nil
  end

  def destroy
    @stakeholder = Stakeholder.find(params[:id])
    @stakeholder.destroy

    render :layout => nil
  end
end
